package com.example.listmoudle.stagger

import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.utils.GridSpaceItemDecoration
import com.example.listmoudle.ImageUtils
import com.example.listmoudle.R
import com.example.listmoudle.arouter.RecyclerPath
import com.example.listmoudle.bean.NumberBean
import com.example.listmoudle.databinding.ActivityListStaggerBinding
import com.example.listmoudle.stagger.adapter.StaggerListAdapter

/**
 * @author: Mr.You
 * @create: 2022-12-01 17:07
 * @description: 瀑布流
 *  * RecycleView实现流式布局
 * 1.取消动画避免滑动时 布局错位
 * 2.涉及动态计算item高度，需要传入外部宽度，然后重新设置给控件。
 * 3.点赞 数据更新UI
 **/
@Route(path = RecyclerPath.RECYCLE_STAGGER_LIST)
class StaggerListActivity: BaseActivity<ActivityListStaggerBinding>() {
    private var mData = mutableListOf<NumberBean>()
    private val mAdapter by lazy {
        StaggerListAdapter((resources.displayMetrics.widthPixels) / 2)
    }


    override fun getLayoutId(): Int = R.layout.activity_list_stagger

    override fun initView() {
        mBinding.recycleView.run {
           // layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL)
            layoutManager = GridLayoutManager(this@StaggerListActivity,2)
            addItemDecoration(GridSpaceItemDecoration(2, 10))
            itemAnimator = null //去除动画  防止刷新时item错位抖动
            adapter = mAdapter
        }
        getDefaultData()
        mAdapter.setData(mData)
    }

    override fun initListener() {
        mBinding.sRefreshLayout.setOnRefreshListener {
            getDefaultData()
            mAdapter.setData(mData)
            mBinding.sRefreshLayout.isRefreshing = false

        }

    }

    private fun getDefaultData() {
        repeat(10) {
            mData.add(NumberBean((Math.random() * 50 + 100).toInt(),ImageUtils.imageList[((Math.random()*50+10)%ImageUtils.imageList.size).toInt()]))
        }
    }
}
